package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.cn.domain.ZMCNCcy;
import com.baosight.sczx.zm.cn.domain.ZMCNMcmodule;
/**
 * 内部抽查样
 */
public class ServiceZMCN32 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "50");//设置每页默认显示记录数
		EiInfo outInfo = this.query(inInfo);
		
		
		return outInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdYear=inInfo.getString("fdYear");
		String fdMonth=inInfo.getString("fdMonth");
		String fdCjnr=inInfo.getString("fdCjnr");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
		}
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdYear", fdYear);
		}
		if(fdMonth==null||fdMonth.trim().equals("")){
			fdMonth=inInfo.getCellStr("inqu_status", 0, "Month");
		}
		if(fdMonth!=null && !fdMonth.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdMonth", fdMonth);
		}
		if(fdCjnr==null||fdCjnr.trim().equals("")){
			fdCjnr=inInfo.getCellStr("inqu_status", 0, "Cjnr");
		}
		if(fdCjnr!=null && !fdCjnr.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdCjnr", fdCjnr);
		}
		
//		String fdYear=inInfo.getString("fdYear");
//		String fdMonth=inInfo.getString("fdMonth");
//		inInfo.setCell("inqu_status", 0, "fdYear", fdYear);
//		inInfo.setCell("inqu_status", 0, "fdMonth", fdMonth);
		EiInfo outInfo = super.query(inInfo, "ZMCNCcy.query", new ZMCNCcy());
		
		outInfo.addBlock(getPartDept()); 
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.ch.status", new String[] { "草拟", "上报", "已发布" }, new String[] { "草拟", "上报", "已发布" }, false));// 状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,"D.10223"));
//		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		outInfo.addBlock(getCoalCode());
		return outInfo;
	}	

	/**
	 * 煤样编码下拉框
	 * @return
	 */
	public EiBlock getCoalCode() {
		EiBlock block = new EiBlock("cmes.ch.coalcode");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			Map<String, String> params=new HashMap();
			params.put("fdStatus", "生效");
			_listData = dao.query("ZMCNMcmodule.query", params);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_map = new HashMap<String, String>();
			ZMCNMcmodule _temp = (ZMCNMcmodule)_listData.get(i);
			_map.put("value", _temp.getFdMyCode());
			_map.put("display",_temp.getFdMyDetail());
			_list.add(_map);
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * 得到质检中心下属分站下拉框
	 * @return
	 */
	public EiBlock getPartDept() {
		EiBlock block = new EiBlock("cmes.ch.partdept");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);
		
		_map = new HashMap<String, String>();
		_map.put("value", "fz001");
		_map.put("display", "中心站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz002");
		_map.put("display", "松藻站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz003");
		_map.put("display", "白岩站");
		_list.add(_map);
		_map = new HashMap<String, String>();
		_map.put("value", "fz004");
		_map.put("display", "金鸡岩站");
		_list.add(_map);

		block.setRows(_list);
		return block;
	}
//	public EiBlock getMines() {
//		EiBlock block = new EiBlock("cmes.ch.mine");
//
//		EiColumn EiColumn = new EiColumn("display");
//		EiColumn.setType("C");
//		block.addMeta(EiColumn);
//
//		EiColumn = new EiColumn("value");
//		EiColumn.setType("C");
//		block.addMeta(EiColumn);
//
//		List<Map> _list = new ArrayList<Map>();
//		Map<String, String> _map = new HashMap<String, String>();
//		_map.put("value", "");
//		_map.put("display", "");
//		_list.add(_map);
//
//		List<Map> _listData = new ArrayList<Map>();
//		try {
//			_listData = dao.query("ZMCNCcyMain.queryTreeChild", null);
//		} catch (Exception e) {}
//		
//		for(int i = 0; i < _listData.size(); i++) {
//			_list.add(_listData.get(i)); 
//		} 
//		block.setRows(_list);
//		return block;
//	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String fdYear=inInfo.getString("fdYear");
		String fdMonth=inInfo.getString("fdMonth");
        String fdCjnr=inInfo.getString("fdCjnr");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
		}
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("result", 0, "fdYear", fdYear);
		}
		if(fdMonth==null||fdMonth.trim().equals("")){
			fdMonth=inInfo.getCellStr("inqu_status", 0, "Month");
		}
		if(fdMonth!=null && !fdMonth.trim().equals("")){
			inInfo.setCell("result", 0, "fdMonth", fdMonth);
		}
		if(fdCjnr==null||fdCjnr.trim().equals("")){
			fdCjnr=inInfo.getCellStr("inqu_status", 0, "Cjnr");
		}
		if(fdCjnr!=null && !fdCjnr.trim().equals("")){
			inInfo.setCell("result", 0, "fdCjnr", fdCjnr);
		}
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_CCY"));
			this.dao.insert("ZMCNCcy.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		String fdYear=inInfo.getString("fdYear");
		String fdMonth=inInfo.getString("fdMonth");
		String fdCjnr=inInfo.getString("fdCjnr");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
		}
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("result", 0, "fdYear", fdYear);
		}
		if(fdMonth==null||fdMonth.trim().equals("")){
			fdMonth=inInfo.getCellStr("inqu_status", 0, "Month");
		}
		if(fdMonth!=null && !fdMonth.trim().equals("")){
			inInfo.setCell("result", 0, "fdMonth", fdMonth);
		}
		if(fdCjnr==null||fdCjnr.trim().equals("")){
			fdCjnr=inInfo.getCellStr("inqu_status", 0, "Cjnr");
		}
		if(fdCjnr!=null && !fdCjnr.trim().equals("")){
			inInfo.setCell("result", 0, "fdCjnr", fdCjnr);
		}
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.update("ZMCNCcy.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCNCcy.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		super.update(inInfo, "ZMCNCcy.report");
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 校验是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-7-29 11:21:08 李红星
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		ZMCNCcy _dom = new ZMCNCcy();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		String fdYear=inInfo.getString("fdYear");
		String fdMonth=inInfo.getString("fdMonth");
		if(fdYear==null||fdYear.trim().equals("")){
			fdYear=inInfo.getCellStr("inqu_status", 0, "Year");
		}
		if(fdYear!=null && !fdYear.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdYear", fdYear);
		}
		if(fdMonth==null||fdMonth.trim().equals("")){
			fdMonth=inInfo.getCellStr("inqu_status", 0, "Month");
		}
		if(fdMonth!=null && !fdMonth.trim().equals("")){
			inInfo.setCell("inqu_status", 0, "fdMonth", fdMonth);
		}
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			String fdBjdw = _dom.getFdBjdw();
			String fdMyNum = _dom.getFdMyNum();
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			params.put("fdBjdw", fdBjdw);
			params.put("fdMyNum", fdMyNum);
			params.put("fdYear", fdYear);
			params.put("fdMonth", fdMonth);
			List result = dao.query("ZMCNCcy.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCNCcy _temp = (ZMCNCcy)result.get(0);
			
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdBjdw() != null && _temp.getFdBjdw().equals(fdBjdw)) {
					msg.append(" 被检单位 ");
				}
				if(_temp.getFdMyNum() != null && _temp.getFdMyNum().equals(fdMyNum)) {
					msg.append(" 煤样编码 ");
				}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
} 